{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import QUANTAXIS as QA\n",
    "from QUANTAXIS.QAARP import QA_Risk, QA_User\n",
    "from QUANTAXIS.QAEngine.QAThreadEngine import QA_Thread\n",
    "from QUANTAXIS.QAUtil.QAParameter import MARKET_TYPE, RUNNING_ENVIRONMENT, ORDER_DIRECTION\n",
    "from QAPUBSUB.consumer import subscriber_topic,  subscriber_routing\n",
    "from QAPUBSUB.producer import publisher_routing\n",
    "from QAStrategy.qactabase import QAStrategyCTABase\n",
    "from QIFIAccount import QIFI_Account, ORDER_DIRECTION\n",
    "from easyquant import EasyMq\n",
    "from easyquant import MongoIo\n",
    "import talib as tdx\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stocklist_all=['600718']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = QA.QA_fetch_stock_day_adv(stocklist_all,'2018-01-01','2020-11-30').data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ch= data.close[-1] * 1.1\n",
    "cl= data.close[-1] * 0.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.close[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2=pd.concat([QA.MA(data.close, x) for x in (5,10,20,30,60,90,120,250) ], axis = 1).dropna()[-1:]\n",
    "df2.columns = [u'm5',u'm10',u'm20',u'm30',u'm60',u'm90',u'm120', u'm250']  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_h = df2.apply(lambda x:x.max() <= ch,  axis = 1 )\n",
    "df_l = df2.apply(lambda x:x.min() >= cl,  axis = 1 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2['dfh'] = df_h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2['dfl'] = df_l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa = df2.iloc[-1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ma = df2.iloc[-1:].apply(lambda x:x.max() < 12,  axis = 1 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mb = df2.iloc[-1:].apply(lambda x:x.min() > 12.5,  axis = 1 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa['ma'] = ma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa['mb'] = mb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cc=aa.append(bb).apply(lambda x : x == True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cc.apply(lambda x : x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp1= lambda x:x+1 if  x>1 else x-1 if x < -4 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp1(-5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa=df2.iloc[-2:-1] #.apply(lambda x: True if x>11 and x < 12 else False, axis=1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(aa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.DataFrame(aa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2.m5.apply(lambda x: True if x>20 else False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2.apply(lambda x: True if x>0 else False, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user = QA_User(username=\"admin\", password='admin')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "port = user.new_portfolio('test-portfolio')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "port"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "code=\"000001\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "code=code[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc = QIFI_Account(\"admin\", \"admin\")\n",
    "acc.initial()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(acc.message)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc.log(acc.message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "codelist = ['600756']\n",
    "stock_names = QA.QA_fetch_stock_name(codelist)\n",
    "stock_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "codename = [stock_names.at[code] for code in codelist]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(stock_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_names.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[stock_names.at[code] for code in codelist]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime, date\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mongo=MongoIo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=m.get_index_day('515050')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d15m= m.get_index_min(code='515050')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d15m.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "code='515050'\n",
    "data_min = mongo.get_index_min(code=code,type='5min')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "date.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_min.index[-1].timestamp()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(time.time() - data_min.index[-1].timestamp()) / 60"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.Timestamp(datetime.now()) - data_min.index[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "abc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datetime.combine(date.today(), datetime.now.time())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datatime.now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datetime.now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from easyquant import QATdx as qatdx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start=data_min.index[-1].strftime('%Y-%m-%d %H:%M:01') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start=start1.strftime('%Y-%m-%d %H:%M:01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "delta_min=5\n",
    "add_df=qatdx.QA_fetch_get_index_min(code,start=start,end='2030-12-31', frequence='%dmin' % delta_min)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "add_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "abc=data_min.append(add_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_min.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "add_df.drop(['date_stamp','datetime'],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "add_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_min.append(add_df, sort=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qatdx.QA_fetch_get_stock_min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from easyquant import MongoIo\n",
    "from easyquant.indicator.base import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mongo = MongoIo()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=mongo.get_stock_day('000859')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.tail(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date        code  \n",
       "2018-01-02  000859    0\n",
       "2018-01-03  000859    0\n",
       "2018-01-04  000859    0\n",
       "2018-01-05  000859    0\n",
       "2018-01-08  000859    0\n",
       "2018-01-09  000859    0\n",
       "2018-01-10  000859    0\n",
       "2018-01-11  000859    0\n",
       "2018-01-12  000859    0\n",
       "2018-01-15  000859    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tdx_skdj_lstd(data)[0][:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tdx_skdj_lstd(data):\n",
    "    LOW = data.low\n",
    "    CLOSE = data.close\n",
    "    HIGH = data.high\n",
    "\n",
    "    N=9\n",
    "    M=3\n",
    "    LOWV=LLV(LOW,N)\n",
    "    HIGHV=HHV(HIGH,N)\n",
    "    RSV=EMA((CLOSE-LOWV)/(HIGHV-LOWV)*100,M)\n",
    "    K=EMA(RSV,M)\n",
    "    D=MA(K,M)\n",
    "    JC1=CROSS(K,D)\n",
    "    \n",
    "    MC=CROSS(D,K)\n",
    "    \n",
    "    # VAR1=(HIGH+LOW+CLOSE*2)/4\n",
    "    # VAR2=EMA(VAR1,21)\n",
    "    # VAR3=STD(VAR1,21)\n",
    "    # VAR4=((VAR1-VAR2)/VAR3*100+200)/4\n",
    "    # VAR5=(EMA(VAR4,89)-25)*1.56\n",
    "    # VAR6=EMA(VAR5,5)*1.22\n",
    "    # VAR7=EMA(VAR6,3)\n",
    "    # VAR8=3*VAR6-2*VAR7\n",
    "    \n",
    "    # VAR1A=IFAND3(CROSS(VAR6,VAR8), CROSS(VAR7,VAR8), CROSS(VAR7,VAR6),1,0)\n",
    "    VAR1B=EMA(CLOSE,3)-EMA(CLOSE,89)\n",
    "    VAR1C=EMA(VAR1B,21)\n",
    "    VAR1D=(VAR1B-VAR1C)*10\n",
    "    VAR1F=POW(VAR1D,3)*0.1+POW(VAR1D,2)\n",
    "    游资=IF(VAR1D>0.015,VAR1F,0)/45\n",
    "    # STICKLINE(VAR1D>0.015,VAR1F/45,0,1,0),COLORRED,\n",
    "    VAR9=EMA(CLOSE,2)-EMA(CLOSE,55)\n",
    "    VAR10=EMA(VAR9,13)\n",
    "    VAR11=2*(VAR9-VAR10)\n",
    "    资金=POW(VAR11,3)*0.1+POW(VAR11,1)\n",
    "    \n",
    "    JC2 = IFAND(游资 > 0, REF(游资, 1) == 0, 1, 0)\n",
    "    JC3 = CROSS(资金, 0)\n",
    "    # 强弱分界:0,COLORGREEN\n",
    "    SSS=9 \n",
    "    LLL=34 \n",
    "    DIF=EMA(CLOSE,12)-EMA(CLOSE,26)\n",
    "    DEA=EMA(DIF,200)\n",
    "    AA=(DIF-DEA)*2\n",
    "    黄金钻信号=IF(AA<0,0,AA*AA)\n",
    "    # {WWW.GUHAI.COM.CN}\n",
    "    DIF1=EMA(CLOSE,9)-EMA(CLOSE,34)\n",
    "    DEA1=EMA(DIF1,200)\n",
    "    AAA=(DIF1-DEA1)*2\n",
    "    启动引导线=IF(AA<0,0,AAA*AAA)\n",
    "    红码=MA(CLOSE,15)+2*STD(CLOSE,15)\n",
    "    XX=EMA(CLOSE,5)\n",
    "    \n",
    "    # DRAWICON(C>红码 AND C>=O ,资金,26)\n",
    "    \n",
    "    # XG = IFAND(JC1, JC2, 1, 0)\n",
    "    XG = IFAND3(IFOR(JC1, REF(JC1,1), True, False), JC2, JC3, 1, 0)\n",
    "    return XG, -1, False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date        code  \n",
       "2021-05-07  000859    0\n",
       "2021-05-10  000859    1\n",
       "2021-05-11  000859    1\n",
       "2021-05-12  000859    0\n",
       "2021-05-13  000859    0\n",
       "2021-05-14  000859    0\n",
       "2021-05-17  000859    0\n",
       "2021-05-18  000859    0\n",
       "2021-05-19  000859    0\n",
       "2021-05-20  000859    0\n",
       "2021-05-21  000859    0\n",
       "2021-05-24  000859    0\n",
       "2021-05-25  000859    0\n",
       "2021-05-26  000859    1\n",
       "2021-05-27  000859    1\n",
       "2021-05-28  000859    0\n",
       "2021-05-31  000859    0\n",
       "2021-06-01  000859    0\n",
       "2021-06-02  000859    0\n",
       "2021-06-03  000859    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IFOR(JC1, REF(JC1,1), 1, 0)[-20:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date        code  \n",
       "2021-05-07  000859    0\n",
       "2021-05-10  000859    0\n",
       "2021-05-11  000859    0\n",
       "2021-05-12  000859    0\n",
       "2021-05-13  000859    0\n",
       "2021-05-14  000859    0\n",
       "2021-05-17  000859    0\n",
       "2021-05-18  000859    0\n",
       "2021-05-19  000859    0\n",
       "2021-05-20  000859    0\n",
       "2021-05-21  000859    0\n",
       "2021-05-24  000859    0\n",
       "2021-05-25  000859    0\n",
       "2021-05-26  000859    0\n",
       "2021-05-27  000859    1\n",
       "2021-05-28  000859    0\n",
       "2021-05-31  000859    0\n",
       "2021-06-01  000859    0\n",
       "2021-06-02  000859    0\n",
       "2021-06-03  000859    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "XG[-20:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date        code  \n",
       "2021-05-07  000859    1\n",
       "2021-05-10  000859    0\n",
       "2021-05-11  000859    0\n",
       "2021-05-12  000859    0\n",
       "2021-05-13  000859    0\n",
       "2021-05-14  000859    0\n",
       "2021-05-17  000859    0\n",
       "2021-05-18  000859    0\n",
       "2021-05-19  000859    0\n",
       "2021-05-20  000859    0\n",
       "2021-05-21  000859    0\n",
       "2021-05-24  000859    0\n",
       "2021-05-25  000859    0\n",
       "2021-05-26  000859    0\n",
       "2021-05-27  000859    1\n",
       "2021-05-28  000859    0\n",
       "2021-05-31  000859    0\n",
       "2021-06-01  000859    0\n",
       "2021-06-02  000859    0\n",
       "2021-06-03  000859    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "JC3[-20:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
